<template>
  <div class="login">
    <div class="login-main">
        <div class="login-main-title">
          <span>黑马大事件后台管理系统</span>
        </div>
        <div class="login-main-content">
          <el-form :model="loginInfo" :rules="rules" status-icon ref="loginForm" size="medium">
            <el-form-item prop="userAccount">
              <el-input v-model="loginInfo.userAccount" placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item prop="userPassword">
              <el-input v-model="loginInfo.userPassword" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="login" style="width:350px;margin-top:40px">登录</el-button>
            </el-form-item>
            <div class="register" style="float:right;">
              <span>没有账号?<router-link to="/register" style="color:#fff;">去注册</router-link></span>
            </div>
            <span style="font-size:12px;color:#faa;">提示:xiaoxiao12 123456</span>
          </el-form>
        </div>
    </div>
  </div>
</template>

<script>
import {reqLogin,reqUserInfo} from '@/api/user.js';
export default {
    name:"login",
    data(){
      const validateCount = (rule,value,callback)=>{
        if(value.trim() == ""){
          return callback(new Error("账号不能为空!"));
        }else{
          callback();
        }
      }
      const validatePass = (rule,value,callback)=>{
        if(value.trim() == ""){
          return callback(new Error("密码不能为空!"))
        }else{
          callback();
        }
      }
      return {
        loginInfo:{
          userAccount:"",
          userPassword:"",
        },
        //表单校验规则
        rules: {
          userAccount: [
            { validator: validateCount, trigger: 'blur' }
          ],
          userPassword: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      login(){
        this.$refs.loginForm.validate(async(valid) => {
          if (valid) {
            //进行登录
            let res = await reqLogin({username:this.loginInfo.userAccount,password:this.loginInfo.userPassword});
            if(res.code !== 0) return this.$message({type:"warning",message:"账号或密码错误"});
            //存储token:res.token
            this.$store.dispatch('user/set_token',{token:res.token});
            this.$router.push('/home');
            this.$message({type:"info",message:"登录成功"});
            //获取用户基本信息
            let res2 = await reqUserInfo(res.token);
            if(res2.code !== 0) return this.$message({type:"warning",message:"用户信息获取失败"});
            this.$store.dispatch('user/get_userinfo',res2.data);
          } else {
            //验证未通过，阻止登录
            this.$message({type:"warning",message:"请输入账号和密码!"});
          }
        })
      }
    }
}
</script>

<style scoped>
.login{
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background: url(@/assets/image/img_01.jpg) no-repeat;
    background-size: cover;
}
.login .login-main{
    width:500px;
    height:350px;
    background: rgba(114, 114, 114, 0.8);
}
.login .login-main .login-main-title{
  width:100%;
  height: 80px;
  text-align: center;
  line-height: 80px;
  font-size:25px;
  font-weight: bold;
}
.login .login-main .login-main-content{
  width: 70%;
  height: 250px;
  margin: 0 auto;
}
</style>